//package com.maoshi.shop.user.api.controller;
///*
// * Copyright (c) 2018-2999 山东卯时信息科技有限公司 All rights reserved.
// *
// * https://www.gz-maoshi.com/
// *
// * 未经允许，不可做商业用途！
// *
// * 版权所有，侵权必究！
// */
//
//import com.maoshi.shop.bean.app.dto.UserStationDto;
//import com.maoshi.shop.bean.enums.PrizeType;
//import com.maoshi.shop.bean.event.ReceivePrizeEvent;
//import com.maoshi.shop.bean.model.User;
//import com.maoshi.shop.bean.model.UserAddr;
//import com.maoshi.shop.bean.model.UserAddrOrder;
//import com.maoshi.shop.common.exception.MaoshiShopBindException;
//import com.maoshi.shop.score.comment.dto.ScoreLuckDto;
//import com.maoshi.shop.score.comment.model.ScoreLuck;
//import com.maoshi.shop.score.comment.model.ScoreLuckUser;
//import com.maoshi.shop.score.comment.service.ScoreLogService;
//import com.maoshi.shop.score.comment.service.ScoreLuckPrizeService;
//import com.maoshi.shop.score.comment.service.ScoreLuckService;
//import com.maoshi.shop.score.comment.service.ScoreLuckUserService;
//import com.maoshi.shop.security.api.util.SecurityUtils;
//import com.maoshi.shop.service.UserAddrOrderService;
//import com.maoshi.shop.service.UserAddrService;
//import com.maoshi.shop.service.UserService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiOperation;
//import lombok.AllArgsConstructor;
//import ma.glasnost.orika.MapperFacade;
//import org.apache.commons.collections.CollectionUtils;
//import org.springframework.context.ApplicationContext;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//
//@RestController
//@RequestMapping("/p/scoreLuck")
//@Api(tags = "积分抽奖接口")
//@AllArgsConstructor
//public class ScoreLuckController {
//
//    private final ScoreLogService scoreLogService;
//    private final ScoreLuckService scoreLuckService;
//    private final ApplicationContext applicationContext;
//    private final ScoreLuckUserService scoreLuckUserService;
//    private final UserService userService;
//    private final ScoreLuckPrizeService scoreLuckPrizeService;
//    private final MapperFacade mapperFacade;
//    private final UserAddrOrderService userAddrOrderService;
//    private final UserAddrService userAddrService;
//
//
//    @GetMapping("/getScoreLuckPrizes")
//    @ApiOperation(value = "获取幸运抽奖奖品列表", notes = "获取幸运抽奖奖品列表")
//    public ResponseEntity<List<ScoreLuckDto>> getScoreLucks() {
//        return ResponseEntity.ok(scoreLuckService.getScoreLuckPrize());
//    }
//
//    @GetMapping("/raffle")
//    @ApiOperation(value = "积分抽奖接口", notes = "积分抽奖接口")
//    @ApiImplicitParam(name = "addId", value = "地址id", required = true, dataType = "int")
//    public ResponseEntity<ScoreLuckDto> raffle(Long addId) {
//        if(addId == null){
//            throw new MaoshiShopBindException("请先选择或输入地址");
//        }
//        String userId = SecurityUtils.getUser().getUserId();
//        // 订单的地址信息
//        UserAddr userAddr = userAddrService.getUserAddrByUserId(addId, userId);
//        // 把订单地址保存到数据库
//        UserAddrOrder userAddrOrder = mapperFacade.map(userAddr, UserAddrOrder.class);
//        if (userAddrOrder == null) {
//            throw new MaoshiShopBindException("请填写收货地址");
//        }
//        User user = userService.getById(userId);
//        List<ScoreLuck> scoreLucks = scoreLuckService.list();
//        if(CollectionUtils.isEmpty(scoreLucks) || user.getScore() < scoreLucks.get(0).getNeedScore()){
//            throw new MaoshiShopBindException("用户积分不足!");
//        }
//        //获取抽到的奖品信息
//        ScoreLuckDto scoreLuckDto = scoreLuckPrizeService.getPrizeDetailByPrizeId(scoreLuckService.getPrize().getPrizeId());
//        //根据奖品类型发放奖品
//        if(Objects.equals(scoreLuckDto.getPrizeType(), PrizeType.XXCY.value())){
//            return ResponseEntity.ok(scoreLuckDto);
//        }
//        //更新奖品库存
//        if (scoreLuckService.updateStocks(scoreLuckDto.getLuckId(),1) == 0) {
//            scoreLuckService.removeScoreLuckPrize();
//            return ResponseEntity.ok(scoreLuckDto);
//        }
//        //发送领取奖品事件，积分和优惠券
//        applicationContext.publishEvent(new ReceivePrizeEvent(userId,scoreLuckDto.getScore(),
//                scoreLuckDto.getPrizeType(),scoreLuckDto.getCouponId()));
//        //保存订单地址信息
//        userAddrOrder.setUserId(userId);
//        userAddrOrder.setCreateTime(new Date());
//        userAddrOrderService.save(userAddrOrder);
//        // 订单地址id
//        Long addrOrderId = userAddrOrder.getAddrOrderId();
//        //添加日志
//        ScoreLuckUser scoreLuckUser = new ScoreLuckUser();
//        scoreLuckUser.setCreateTime(new Date());
//        scoreLuckUser.setPic(scoreLuckDto.getPic());
//        scoreLuckUser.setProdName(scoreLuckDto.getPrizeName());
//        scoreLuckUser.setStatus(1);
//        //如果是商品类型需要手动发放
//        if(Objects.equals(scoreLuckDto.getPrizeType(),PrizeType.PRODUCT.value())) {
//            scoreLuckUser.setStatus(0);
//        }
//        scoreLuckUser.setUserId(userId);
//        scoreLuckUser.setOrderAddrId(addrOrderId);
//        scoreLuckUserService.save(scoreLuckUser);
//        return ResponseEntity.ok(scoreLuckDto);
//    }
//
//}
